package com.wd.tech.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wd.tech.R;
import com.wd.tech.adapter.CommunityAdapter;
import com.wd.tech.base.BaseFragment;
import com.wd.tech.base.BasePresenter;
import com.wd.tech.bean.AddCommunityGreatBean;
import com.wd.tech.bean.CancelCommunityGreatBean;
import com.wd.tech.bean.FindCommunityListBean;
import com.wd.tech.mvp.IPresenterImpl;
import com.wd.tech.url.BaseUrl;
import com.wd.tech.utils.NetUtil;
import com.wd.tech.view.activity.ReleaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class SheQuFragment extends BaseFragment {

    @BindView(R.id.shequ_recyeler)
    RecyclerView shequRecyeler;
    @BindView(R.id.shequ_sm)
    SmartRefreshLayout shequSm;
    @BindView(R.id.img_rele)
    ImageView imgRele;
    private int pages = 1;
    List<FindCommunityListBean.ResultBean> list = new ArrayList<>();
    private boolean hasNet;

    @Override
    protected void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", 1);
        map.put("count", 10);
        shequSm.setEnableRefresh(true);
        shequSm.setEnableLoadMore(true);
        shequSm.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pages++;
                presenter.IModelCommunityInfo(BaseUrl.CommunitUrl, map, FindCommunityListBean.class);
                shequSm.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                pages = 1;
                presenter.IModelCommunityInfo(BaseUrl.CommunitUrl, map, FindCommunityListBean.class);
                shequSm.finishRefresh();
            }
        });
        hasNet = NetUtil.getInstance().HasNet();
        SheQuFragment sheQuFragment = new SheQuFragment();
        Bundle bundle = new Bundle();
        bundle.putString("str","key");
        sheQuFragment.setArguments(bundle);
    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenterImpl();
    }

    @Override
    protected void initView(View view) {
        shequRecyeler.setLayoutManager(new LinearLayoutManager(getContext()));

        imgRele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ReleaseActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected int layoutId() {
        return R.layout.shequ_fragment;
    }

    @Override
    public void success(Object o) {
        if (hasNet) {
                if (o instanceof FindCommunityListBean && TextUtils.equals("0000", ((FindCommunityListBean) o).getStatus())) {
                    Toast.makeText(getContext(), "0000", Toast.LENGTH_SHORT).show();
                    List<FindCommunityListBean.ResultBean> result = new FindCommunityListBean().getResult();
                    CommunityAdapter communityAdapter = new CommunityAdapter(getActivity(), ((FindCommunityListBean) o).getResult());
                    shequRecyeler.setAdapter(communityAdapter);
                }
            }
         else {
            Toast.makeText(getContext(), "当前没有网络", Toast.LENGTH_SHORT).show();
        }
//        if (o instanceof AddCommunityGreatBean && TextUtils.equals("0000", ((AddCommunityGreatBean) o).getStatus())) {
//            Toast.makeText(getContext(), ((AddCommunityGreatBean) o).getMessage(), Toast.LENGTH_SHORT).show();
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("page",1);
//            map.put("count",10);
//            presenter.IModelCommunityInfo(BaseUrl.CommunitUrl,map,FindCommunityListBean.class);
//        }
    }

    @Override
    public void Error(Throwable throwable) {

    }
}
